class Solution {
    int printIndex = 0;
    public TreeNode buildTreeChild(int[] postorder, int[] inorder, int beginIndex, int endIndex){
        if(beginIndex > endIndex){
            return null;
        }
        int print = postorder[printIndex];
        TreeNode root = new TreeNode(print);
        int inIndex = findIndex(inorder, print);

        printIndex--;
        root.right = buildTreeChild(postorder, inorder, inIndex+1, endIndex);
        root.left = buildTreeChild(postorder, inorder, beginIndex, inIndex-1);
        return root;
    }
    public int findIndex(int[] inorder, int print){
        for(int i= 0;i< inorder.length; i++ ){
            if(print == inorder[i]){
                return i;
            }
        }
        return -1;
    }
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if(inorder == null || postorder == null){
            return null;
        }
        printIndex = postorder.length - 1;
        return buildTreeChild(postorder, inorder, 0,postorder.length - 1);
    }
}